package com.kgc.activityprovider.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.activiti.util.ResponseMsg;
import com.kgc.activityprovider.dao.InStoreWorkFlowMapper;
import com.kgc.activityprovider.feign.store.InStoreDetailFeign;
import com.kgc.activityprovider.feign.store.InStoreFeign;
import com.kgc.activityprovider.service.WorkFlowUtilService;
import com.kgc.store.pojo.InStore;
import com.kgc.store.pojo.InStoreDetail;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InStoreWorkFlowServiceImpl implements WorkFlowUtilService {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String mailAddress;

    @Value("${email.subject}")
    private String emailSubject;

    @Value("${email.template}")
    private String emailTemplate;

    @Value("${email.template.directory}")
    private String templateDirectory;

    @Autowired
    private InStoreFeign inStoreFeign;

    @Autowired
    private InStoreDetailFeign inStoreDetailFeign;


    @Autowired
    private Configuration freemarkerConfiguration;

    @Autowired
    private InStoreWorkFlowMapper inStoreWorkFlowMapper;
    @Override
    public ResponseMsg changeStatus(String code, int status) {
        ResponseMsg msg = new ResponseMsg();
        int changeCount = inStoreWorkFlowMapper.changeStatus(code, status);
        if(changeCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg insertCheck(String code, Integer checkManId, String checkComment) {
        ResponseMsg msg = new ResponseMsg();
        int updateCount = inStoreWorkFlowMapper.insertCheck(code, checkManId, checkComment);
        if(updateCount>0){
            msg.setData(true);
            msg.setCode("200");
        }else{
            msg.setData(false);
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg sendEmail(String code, String emailTo) throws Exception{
        ResponseMsg msg = new ResponseMsg();
        File templateDir = new File(templateDirectory);
        freemarkerConfiguration.setDirectoryForTemplateLoading(templateDir);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(mailAddress);
        helper.setTo("3095929053@qq.com");
        helper.setSubject(emailSubject);


        if (!templateDir.exists() || !templateDir.isDirectory()) {
            throw new Exception("模板目录不存在: " + templateDirectory);
        }
        Map<String,Object> mapInStore = new HashMap<>();
        mapInStore.put("pageNo",1);
        mapInStore.put("pageSize",3);
        mapInStore.put("code",code);
        mapInStore.put("typeId",5);
//        com.kgc.basic.util.ResponseMsg msg = inStoreFeign.getInStoreList(mapInStore);
//        System.out.println("asd");
        ObjectMapper mapper = new ObjectMapper();

        Map<String,Object> page = (Map<String, Object>) inStoreFeign.getInStoreList(mapInStore).getData();
        ArrayList<Map<String, Object>> inStore =(ArrayList<Map<String, Object>>) page.get("list");
//        Map<String,Object> inStoreMap = (Map<String,Object>)msg.getData();
//        List<> list = (ArrayList<> )inStoreMap.get("list");
//        Map<String, Object> stringObjectMap = ;
        InStore inStoreObj = mapper.convertValue(inStore.get(0), InStore.class);

        ArrayList<Map<String, Object>> arrayList = (ArrayList<Map<String, Object>>)inStoreDetailFeign.getInStoreDetailList(code).getData();
//        System.out.println("asd");
//        ArrayList<Map<String, Object>> inStore1 =(ArrayList<Map<String, Object>>) page1.get("list");
        List<InStoreDetail> inStoreDetailList = new ArrayList<>();
        for(Map<String, Object> item:arrayList){
            InStoreDetail inStoreDetail = mapper.convertValue(item, InStoreDetail.class);
            inStoreDetailList.add(inStoreDetail);
        }

        Map<String, Object> model = new HashMap<>();
        model.put("subject",emailSubject);
        model.put("sendMail", mailAddress);
        model.put("url", "https://www.baidu.com/");
        model.put("code", inStoreObj.getCode());
        model.put("providerName", inStoreObj.getProviderName());
        model.put("deptName", inStoreObj.getDeptName());
        model.put("businessmanName", inStoreObj.getBusinessmanName());
        model.put("storeManName", inStoreObj.getStoreManName());
        model.put("typeName", inStoreObj.getTypeName());
        model.put("inStoreSumPrice", inStoreObj.getInStoreSumPrice());
        model.put("appName", "审核");

        model.put("inStoreDetailList", inStoreDetailList);

// 加载并渲染模板
        Template template = freemarkerConfiguration.getTemplate(emailTemplate);
        StringWriter out = new StringWriter();
        template.process(model, out);
        helper.setText(out.toString(), true);

        javaMailSender.send(mimeMessage);
        msg.setCode("200");
        msg.setMsg("发送成功");
        return msg;
    }
}
